package egovframework.npqs.pqi.mobile.menu;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import egovframework.npqs.pqi.mobile.ActivityMain;
import egovframework.npqs.pqi.mobile.BaseFragment;
import egovframework.npqs.pqi.mobile.R;
import egovframework.npqs.pqi.mobile.activity.approve.ApproveList;
import egovframework.npqs.pqi.mobile.activity.disinfection.DisinfectionResultInsert;
import egovframework.npqs.pqi.mobile.activity.disinfection.DisinfectionResultList;
import egovframework.npqs.pqi.mobile.activity.disinfection.DisinfectionResultStatus;
import egovframework.npqs.pqi.mobile.activity.exportquarantineresult.ExportQuarantineResultInsert;
import egovframework.npqs.pqi.mobile.activity.exportquarantineresult.ExportQuarantineResultList;
import egovframework.npqs.pqi.mobile.activity.exportquarantineresult.ExportQuarantineResultStatus;
import egovframework.npqs.pqi.mobile.activity.quarantinerequest.QuarantineRequestList;
import egovframework.npqs.pqi.mobile.activity.quarantineresult.QuarantineResultList;
import egovframework.npqs.pqi.mobile.activity.quarantineresult.QuarantineResultInsert;
import egovframework.npqs.pqi.mobile.activity.quarantineresult.QuarantineResultStatus;

public class MenuControl extends ListFragment implements OnItemClickListener
{
	
	static ActivityMain am;
	public static BaseFragment tagetFragment = null;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.list, null);
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		am = (ActivityMain) ActivityMain.am;
		
		MenuAdapter adapter = new MenuAdapter(getActivity());
		
		//검역신청
		MenuItem titleMenu1 = new MenuItem( R.drawable.left_menu01_selector);
		//수입검역결과
		MenuItem titleMenu2 = new MenuItem( R.drawable.menuall02);
		MenuItem slaveMenu2_1 = new MenuItem( R.drawable.left_menu02_01_selector);
		MenuItem slaveMenu2_2 = new MenuItem( R.drawable.left_menu02_02_selector);
		MenuItem slaveMenu2_3 = new MenuItem( R.drawable.left_menu02_03_selector);
		//소독결과
		MenuItem titleMenu3 = new MenuItem( R.drawable.menuall03);
		MenuItem slaveMenu3_1 = new MenuItem( R.drawable.left_menu03_01_selector);
		MenuItem slaveMenu3_2 = new MenuItem( R.drawable.left_menu03_02_selector);
		MenuItem slaveMenu3_3 = new MenuItem( R.drawable.left_menu03_03_selector);
		//결재관리
		MenuItem titleMenu4 = new MenuItem( R.drawable.left_menu04_selector);
		//수출검역결과
		MenuItem titleMenu5 = new MenuItem( R.drawable.menuall06);
		MenuItem slaveMenu5_1 = new MenuItem( R.drawable.left_menu02_01_selector);
		MenuItem slaveMenu5_2 = new MenuItem( R.drawable.left_menu02_02_selector);
		MenuItem slaveMenu5_3 = new MenuItem( R.drawable.left_menu02_03_selector);
		
		adapter.add(titleMenu1);
		adapter.add(titleMenu2);
		adapter.add(slaveMenu2_1);
		adapter.add(slaveMenu2_2);
		adapter.add(slaveMenu2_3);
		adapter.add(titleMenu3);
		adapter.add(slaveMenu3_1);
		adapter.add(slaveMenu3_2);
		adapter.add(slaveMenu3_3);
		adapter.add(titleMenu4);
		adapter.add(titleMenu5);
		adapter.add(slaveMenu5_1);
		adapter.add(slaveMenu5_2);
		adapter.add(slaveMenu5_3);
		
		/*adapter.add(new MenuItem( R.drawable.left_menu01_selector));
		adapter.add(new MenuItem( R.drawable.menuall02));
		adapter.add(new MenuItem( R.drawable.left_menu02_01_selector));
		adapter.add(new MenuItem( R.drawable.left_menu02_02_selector));
		adapter.add(new MenuItem( R.drawable.left_menu02_03_selector));
		adapter.add(new MenuItem( R.drawable.menuall03));
		adapter.add(new MenuItem( R.drawable.left_menu03_01_selector));
		adapter.add(new MenuItem( R.drawable.left_menu03_02_selector));
		adapter.add(new MenuItem( R.drawable.left_menu03_03_selector));
		adapter.add(new MenuItem( R.drawable.left_menu04_selector));
		adapter.add(new MenuItem( R.drawable.menuall06));
		adapter.add(new MenuItem( R.drawable.left_menu02_01_selector));
		adapter.add(new MenuItem( R.drawable.left_menu02_02_selector));
		adapter.add(new MenuItem( R.drawable.left_menu02_03_selector));*/
		
		setListAdapter(adapter);
		
		ListView lv = getListView();
		lv.setOnItemClickListener(this);
		lv.setDividerHeight(0);
		lv.setDivider(null);
	}

	private class MenuItem
	{
		public String tag;
		public int iconRes;

	/*	public MenuItem(String tag, int iconRes) {
			this.tag = tag;
			this.iconRes = iconRes;
		}*/
		public MenuItem( int iconRes) {
			this.iconRes = iconRes;
		}
	}

	public class MenuAdapter extends ArrayAdapter<MenuItem>
	{

		public MenuAdapter(Context context) {
			super(context, 0);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
			}
			ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
			//icon.setImageResource(getItem(position).iconRes);
			icon.setBackgroundResource(getItem(position).iconRes);
			/*TextView title = (TextView) convertView.findViewById(R.id.row_title);
			title.setText(getItem(position).tag);*/

			return convertView;
		}

	}
	
	
	public void onItemClick(AdapterView<?> arg0, View view, int pos, long id) {
		Intent it;
		switch (pos) {
		case 0:
//			clearBackStack();
			/*FragmentTransaction ft = getFragmentManager().beginTransaction();
			if(tagetFragment != null){
				ft.remove(tagetFragment);
				tagetFragment = null;
			}
			ft.commit();		
			am.toggle();*/
			QuarantineRequestList quarantineRequest = new QuarantineRequestList();
			addFragmentReplace(R.id.base_frame, quarantineRequest, QuarantineRequestList.class.getName());
			am.toggle();
			break;
		case 2:
			QuarantineResultList incomeResultList = new QuarantineResultList();
			addFragmentReplace(R.id.base_frame, incomeResultList, QuarantineResultList.class.getName());
			am.toggle();
			break;			
		case 3:
			QuarantineResultInsert quarantineResultInsert = new QuarantineResultInsert();
			addFragmentReplace(R.id.base_frame, quarantineResultInsert, QuarantineResultInsert.class.getName());
			am.toggle();
			//it = new Intent(getActivity(), ActivityMainVATReport.class);
			//startActivity(it);
			break;			
		case 4:
			QuarantineResultStatus quarantineResultStatus = new QuarantineResultStatus();
			addFragmentReplace(R.id.base_frame, quarantineResultStatus, QuarantineResultStatus.class.getName());
			am.toggle();
			/*it = new Intent(getActivity(), ActivityMainFranchise.class);
			startActivity(it);*/
			break;
		case 6:
			DisinfectionResultList disinfectionResultList = new DisinfectionResultList();
			addFragmentReplace(R.id.base_frame, disinfectionResultList, DisinfectionResultList.class.getName());
			am.toggle();
			
			break;			
		case 7:
			DisinfectionResultInsert disinfectionResultInsert = new DisinfectionResultInsert();
			addFragmentReplace(R.id.base_frame, disinfectionResultInsert, DisinfectionResultInsert.class.getName());
			am.toggle();
			/*it = new Intent(getActivity(), ActivityMainSetting.class);
			startActivity(it);*/
			
			break;			
		case 8:
			DisinfectionResultStatus disinfectionResultStatus = new DisinfectionResultStatus();
			addFragmentReplace(R.id.base_frame, disinfectionResultStatus, DisinfectionResultStatus.class.getName());
			am.toggle();
			/*it = new Intent(getActivity(), ActivityMainSetting.class);
			startActivity(it);*/
			break;
		case 9:			
			ApproveList approveList = new ApproveList();
			addFragmentReplace(R.id.base_frame, approveList, ApproveList.class.getName());
			am.toggle();
			/*it = new Intent(getActivity(), ActivityMainSetting.class);
			startActivity(it);*/
			break;
			
		case 11:
			//Toast.makeText(getActivity().getApplicationContext(), "구현중입니다.", Toast.LENGTH_SHORT).show();
			ExportQuarantineResultList exportQuarantineResultList = new ExportQuarantineResultList();
			addFragmentReplace(R.id.base_frame, exportQuarantineResultList, DisinfectionResultStatus.class.getName());
			am.toggle();
			break;
			
		case 12:
			ExportQuarantineResultInsert exportQuarantineResultInsert = new ExportQuarantineResultInsert();
			addFragmentReplace(R.id.base_frame, exportQuarantineResultInsert, ExportQuarantineResultInsert.class.getName());
			am.toggle();
			break;
			
		case 13:
			ExportQuarantineResultStatus exportQuarantineResultStatus = new ExportQuarantineResultStatus();
			addFragmentReplace(R.id.base_frame, exportQuarantineResultStatus, ExportQuarantineResultStatus.class.getName());
			am.toggle();
			break;
		}
	}
	
	
	public void clearBackStack(){
		FragmentManager fragmentManager = getFragmentManager();
		fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
	}
	
	public void addFragmentReplace(int layoutId, BaseFragment baseFragment, String name) {
		tagetFragment = baseFragment;
		//clearBackStack();
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.replace(layoutId, baseFragment, name);
		ft.commit();			
	}	
}
